Dialog = ClassUtils:extends({}, Display);

function Dialog:ctor(...)
    self.popup = true;
    self.popdown = true;
    self.popupTween = nil;
    self.popdownTween = nil;
    self.popdownCompleteFun = nil;
    self.delayDestroyHandler = nil;
    Display.ctor(self,...);
end

function Dialog:show(prefabPath)
    if prefabPath ~= nil then
        self:setPrefabPath(prefabPath);
    end
    DialogMgr:show(self);
end

function Dialog:hide(popdownCompleteFun)
    self.popdownCompleteFun = popdownCompleteFun;
    if self.popdown and self.isInit then
        self:popdownAnimation();
    else
        DialogMgr:hide(self);
    end
end

function Dialog:loadPrefabComplete()
    Display.loadPrefabComplete(self);
    if self.visible then
        self:clearPopTween();
        self:popupAnimation();
    end
end

function Dialog:setVisible(visible)
    self.visible = visible;
    if not self.isInit then
        return;
    end
    local activeSelf = self.gameObject.activeSelf;
    Display.setVisible(self,visible);
    if visible and not activeSelf then
        self:clearPopTween();
        self:popupAnimation();
    elseif not visible and activeSelf then
        self:clearPopTween();
    end
end

function Dialog:onMaskClick()
    self:hide();
end

function Dialog:popupAnimation()
    if self.popup then
        self.gameObject.transform.localScale = Vector3.zero;
        self.popupTween = self.gameObject.transform:DOScale(Vector3.one, 0.267):SetEase(Ease.OutQuad);
    end
end

function Dialog:popdownAnimation()
    if not self.popdownTween then
        self.popdownTween = self.gameObject.transform:DOScale(Vector3.zero, 0.234):SetEase(Ease.OutCubic):OnComplete(function()
            if self.popdownCompleteFun then
                self.popdownCompleteFun();
            end
            DialogMgr:hide(self);
        end);
    end
end

function Dialog:clearPopTween()
    if self.popupTween then
        self.popupTween:Kill();
        self.popupTween = nil;
    end
    if self.popdownTween then
        self.popdownTween:Kill();
        self.popdownTween = nil;
    end
    self.popdownCompleteFun = nil;
end